<?php

declare(strict_types=1);

namespace App\Helper;

use App\Constants\ErrorCode;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;

class ResponseHelper
{
    /**
     * 成功响应
     *
     * @param mixed $data 返回数据
     * @param string $message 提示信息
     * @param int $code 业务状态码
     * @return PsrResponseInterface
     */
    public static function success(mixed $data = [], string $message = 'Success', int $code = ErrorCode::SUCCESS): PsrResponseInterface
    {
        return self::json($code, $message, $data);
    }

    /**
     * 失败响应
     *
     * @param string $message 错误信息
     * @param int $code 错误码
     * @param mixed $data 附加数据
     * @return PsrResponseInterface
     */
    public static function fail(string $message = 'Operation Failed', int $code = ErrorCode::BAD_REQUEST, mixed $data = []): PsrResponseInterface
    {
        return self::json($code, $message, $data);
    }

    /**
     * 标准 JSON 响应
     *
     * @param int $code 业务状态码
     * @param string $message 提示信息
     * @param mixed $data 返回数据
     * @param int $httpCode HTTP 状态码
     * @return PsrResponseInterface
     */
    public static function json(int $code, string $message, mixed $data = [], int $httpCode = 200): PsrResponseInterface
    {
        $response = ApplicationContext::getContainer()->get(ResponseInterface::class);

        $result = [
            'code' => $code,
            'error_message' => $message,
            'data' => $data ?: [],
        ];

        return $response->json($result)->withStatus($httpCode);
    }

    /**
     * 分页响应
     *
     * @param array $items 数据列表
     * @param int $total 总数
     * @param int $page 当前页
     * @param int $pageSize 每页数量
     * @return PsrResponseInterface
     */
    public static function paginate(array $items, int $total, int $page = 1, int $pageSize = 15): PsrResponseInterface
    {
        $data = [
            'items' => $items,
            'pagination' => [
                'total' => $total,
                'page' => $page,
                'page_size' => $pageSize,
                'total_pages' => (int) ceil($total / $pageSize),
            ],
        ];

        return self::success($data);
    }

    /**
     * 创建响应
     *
     * @param string $message 提示信息
     * @return PsrResponseInterface
     */
    public static function created(string $message = 'Created Successfully'): PsrResponseInterface
    {
        return self::json(ErrorCode::SUCCESS, $message, [], 201);
    }

    /**
     * 无内容响应
     *
     * @return PsrResponseInterface
     */
    public static function noContent(): PsrResponseInterface
    {
        $response = ApplicationContext::getContainer()->get(ResponseInterface::class);
        return $response->withStatus(204);
    }

    /**
     * 未授权响应
     *
     * @param string $message
     * @return PsrResponseInterface
     */
    public static function unauthorized(string $message = 'Unauthorized'): PsrResponseInterface
    {
        return self::json(ErrorCode::UNAUTHORIZED, $message, [], 401);
    }

    /**
     * 禁止访问响应
     *
     * @param string $message
     * @return PsrResponseInterface
     */
    public static function forbidden(string $message = 'Forbidden'): PsrResponseInterface
    {
        return self::json(ErrorCode::FORBIDDEN, $message, [], 403);
    }

    /**
     * 未找到响应
     *
     * @param string $message
     * @return PsrResponseInterface
     */
    public static function notFound(string $message = 'Not Found'): PsrResponseInterface
    {
        return self::json(ErrorCode::NOT_FOUND, $message, [], 404);
    }

    /**
     * 验证失败响应
     *
     * @param array $errors 验证错误信息
     * @param string $message
     * @return PsrResponseInterface
     */
    public static function validationError(array $errors = [], string $message = 'Validation Failed'): PsrResponseInterface
    {
        return self::json(ErrorCode::VALIDATION_ERROR, $message, $errors, 422);
    }

    /**
     * 服务器错误响应
     *
     * @param string $message
     * @return PsrResponseInterface
     */
    public static function serverError(string $message = 'Internal Server Error'): PsrResponseInterface
    {
        return self::json(ErrorCode::SERVER_ERROR, $message, [], 500);
    }
}

